﻿using System;
using System.Collections.Generic;
using Tss.MagicWeb.Domain.Cards;
using Tss.MagicWeb.Domain.Entities;

namespace Tss.MagicWeb.Domain.Decks
{
    public static class DeckValidator
    {
        public static DeckValidationResult Validate(Deck deck)
        {
            var messages = new List<String>();

            foreach (var deckItem in deck.DeckItems)
            {
                var card = PhysicalCardFactory.CreatePhysicalCard(deckItem.CardGuid);
                if (card != null)
                {
                    var validationRuleProvider = card.GetDeckValidationRuleProvider();
                    var result = validationRuleProvider.GetValidationRule().Invoke(deck, card);
                    if (!result.Valid)
                    {
                        messages.Add(result.ErrorMessage);
                    }
                }
            }

            // 100.2a In constructed play each deck must contain at least sixty cards.
            var globalValidationProvider = deck.GetDeckValidationRuleProvider();
            var globalResult = globalValidationProvider.GetValidationRule().Invoke(deck, null);
            if (!globalResult.Valid)
            {
                messages.Add(globalResult.ErrorMessage);
            }

            return new DeckValidationResult(messages);
        }
    }
}
